package com.wejoy.video.middle.station.admin.controller.detection;

import com.wejoy.video.middle.station.admin.controller.BaseController;
import com.wejoy.video.middle.station.admin.util.CurrentUserUtils;
import com.wejoy.video.middle.station.data.annotation.RoleCheck;
import com.wejoy.video.middle.station.data.domain.PageResult;
import com.wejoy.video.middle.station.data.domain.detection.DetectionIntervar;
import com.wejoy.video.middle.station.data.domain.system.SysUser;
import com.wejoy.video.middle.station.data.service.detection.IDetectionIntervarService;
import com.wejoy.video.middle.station.data.util.WJAssert;
import com.wejoy.video.middle.station.data.vo.RespJSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.ArrayList;

/**
 * 报警检测间隔
 *
 * @author miracle_j
 * @date 2024年04月08日 17:40:43
 */
@RestController
@RequestMapping("/detection/detectionIntervar")
public class DetectionIntervarController extends BaseController {

    @Autowired
    private IDetectionIntervarService detectionIntervarService;

    /**
     * 保存或更新
     *
     * @param detectionIntervarReq detectionIntervarReq
     * @return RespJSON
     */
    @PostMapping("/save")
    @RoleCheck(name = "detection:add,detection:edit")
    public RespJSON save(@RequestBody DetectionIntervar detectionIntervarReq, @SessionAttribute(CurrentUserUtils.SYSTEM_USER) SysUser loginSysUser) {
        detectionIntervarService.save(detectionIntervarReq, loginSysUser.getName());
        return RespJSON.success();
    }

    /**
     * 分页查询
     *
     * @param detectionIntervarReq detectionIntervarReq
     * @return RespJSON
     */
    @PostMapping("/getPageList")
    @RoleCheck(name = "detection:list")
    public RespJSON getPageList(@RequestBody DetectionIntervar detectionIntervarReq) {
        int count = detectionIntervarService.getCount(detectionIntervarReq);
        List<DetectionIntervar> list = new ArrayList<>();
        if (count > 0) {
            list = detectionIntervarService.getPageList(detectionIntervarReq);
        }
        PageResult result = new PageResult(detectionIntervarReq.getPageNo(), detectionIntervarReq.getPageSize(), count, list);
        return RespJSON.success(result);
    }

    /**
     * 主键查询
     *
     * @param id id
     * @return RespJSON
     */
    @RequestMapping("/getOneById")
    public RespJSON getOneById(Long id) {
        return RespJSON.success(detectionIntervarService.getOneById(id));
    }

    /**
    * 审核
    *
    * @param id id
    * @return RespJSON
    */
    @PostMapping("/audit")
    @ResponseBody
        public RespJSON audit(Long id, @SessionAttribute(CurrentUserUtils.SYSTEM_USER)SysUser loginSysUser) {
        WJAssert.notNull(id, "ID 不合法");
        detectionIntervarService.audit(id, loginSysUser.getUserName());
        return RespJSON.success();
    }

    /**
    * 取消审核
    *
    * @param id id
    * @return RespJSON
    */
    @PostMapping("/unaudit")
    @ResponseBody
        public RespJSON unaudit(Long id, @SessionAttribute(CurrentUserUtils.SYSTEM_USER)SysUser loginSysUser) {
        WJAssert.notNull(id, "ID 不合法");
        detectionIntervarService.unaudit(id, loginSysUser.getUserName());
        return RespJSON.success();
    }

}
